/**
 * 
 */
package truerefactor.genetic;

/**
 * Fitness - Provides a containing location for an Idividual's total fitness as
 * well as the components which make up that fitness value
 * 
 * @author Isaac Griffith
 */
public class Fitness {

    /**
     * Storage container for the individual component values of the fitness
     * value
     */
    private double individualVals[];
    /**
     * the running summation of the individual values
     */
    private double total = 0;
    /**
     * current index for the next value to be inserted
     */
    private int index = 0;

    /**
     * Creates a new instance of Fitness with the maximum number of values set
     * to the provided value.
     * 
     * @param maxIndex
     *            Maximum number of values to be contained by this object
     */
    public Fitness(int maxIndex)
    {
        individualVals = new double[maxIndex];

        for (int i = 0; i < maxIndex; i++)
        {
            individualVals[i] = 0;
        }
    }

    /**
     * @return An array containing the individual components of this fitness
     *         value.
     */
    public double[] getIndividualVals()
    {
        return individualVals;
    }

    /**
     * Adds an individual value to this fitness calculation. If the maximum
     * number of values has been reached then nothing occurs
     * 
     * @param value
     *            value to be included in the fitness calculation
     */
    public void addIndividualVal(double value)
    {
        if (index < individualVals.length)
        {
            individualVals[index] = value;
            total += value;
            index++;
        }
    }

    /**
     * @return the summation of the individual fitness values
     */
    public double getTotal()
    {
        return total;
    }
}
